package kr.hkit.powerjava.chapter17;

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;

public class CheckBox extends JPanel implements ItemListener {

	JCheckBox[] chkBtns = new JCheckBox[3];
	String[] strs = { "111", "222", "333" };
	JLabel[] lbls = new JLabel[3];
	ImageIcon[] icons = new ImageIcon[3];
	private JPanel panelChk;

	public CheckBox() {

		initalize();
	}

	private void initalize() {

		setLayout(new GridLayout(0, 4));

		panelChk = new JPanel(new GridLayout(0, 1));

		for (int i = 0; i < 3; i++) {
			chkBtns[i] = new JCheckBox(strs[i]);
			chkBtns[i].addItemListener(this);
			lbls[i] = new JLabel(strs[i] + ".jpg");
			icons[i] = new ImageIcon(strs[i] + ".jpg");

			panelChk.add(chkBtns[i]);

		}

		add(panelChk);

		add(lbls[0]);
		add(lbls[1]);
		add(lbls[2]);

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		ImageIcon image = null;

		Object source = e.getItemSelectable();
		for (int i = 0; i < 3; i++) {

			if (source == chkBtns[i]) {
				if (e.getStateChange() == ItemEvent.DESELECTED) {
					lbls[i].setIcon(null);
				} else {
					lbls[i].setIcon(icons[i]);
				}
			}
		}

	}

}
